package com.yugao.fintech.antelope.mps.core.sender;

import com.yugao.fintech.antelope.mps.api.constatnts.ChannelTypeEnum;
import com.yugao.fintech.framework.assistant.utils.exception.Assert;
import com.yugao.fintech.framework.assistant.utils.exception.BizException;
import com.yugao.fintech.framework.core.utils.SpringUtils;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 消息发送处理器
 */
@Component
public class MsgSendManager {
    private static final Map<ChannelTypeEnum, MsgSender> msgSendHandlerMap = new ConcurrentHashMap<>();

    public static void init() {
        msgSendHandlerMap.clear();
        Map<String, MsgSender> beansOfType = SpringUtils.getBeansOfType(MsgSender.class);
        beansOfType.forEach((k, v) -> {
            Assert.notNull(v.channelType(), "bean " + k + " channelType() method return null");
            msgSendHandlerMap.put(v.channelType(), v);
        });
    }

    public MsgSender getMsgSendHandler(ChannelTypeEnum channelType) {
        MsgSender msgSender = msgSendHandlerMap.get(channelType);
        if (Objects.isNull(msgSender)) {
            throw new BizException("channelType {} 未实现响应的发送处理器", channelType);
        }
        return msgSender;
    }

}
